package com.example.demo.school.controllers;

import com.example.demo.school.models.StStudent;
import com.example.demo.school.models.StType;
import com.example.demo.school.repositories.StStudentRepository;
import com.example.demo.school.repositories.StTypeRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.*;
import org.json.*;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;


/**
 * @Author Jack
 * @Package com.example.demo.school.controllers
 * @Date 4/11/2024 2:07 PM
 * @description:  为方便查阅直接在Controller层实现逻辑
 */
@RestController
@RequestMapping("/api")
public class AllController {

    @Autowired
    StTypeRepository stTypeRepository;

    @Autowired
    StStudentRepository studentRepository;

    /** 学历列表 **/
    @GetMapping("/typeList")
    @ResponseBody
    public List<?>  typeList(@RequestParam("name")String name){
        name = (name == null || name.equals("undefined")) ? "" : name;
        return stTypeRepository.findByNameLike("%"+name+"%");
    }

    /** 学历新增修改 **/
    @PostMapping("/typeSave")
    public int saveType(@RequestBody StType type) {
        if(Objects.equals(type.getName(), "") || type.getName()==null || type.getName().equals("undefined")){
            return 501;
        }
        try {
            stTypeRepository.save(type);
        } catch (Exception e) {
            return 500;
        }
        return 200;
    }

    /** 学历删除 **/
    @PostMapping("/typeRemove")
    @ResponseBody
    public int typeRemove(@RequestParam("ids") Long[] ids){
        try {
            stTypeRepository.deleteByIds(Arrays.asList(ids));
        } catch (DataIntegrityViolationException e){
            SQLException sqlException = (SQLException) e.getCause().getCause(); // 获取SQL异常
            return sqlException.getErrorCode(); //数据库错误码
        } catch (Exception e){
            return 500;
        }
        return 200;
    }

    /** 学生列表 **/
    @GetMapping("/studList")
    @ResponseBody
    public List<?>  studList(@RequestParam("name")String name,@RequestParam("sex")String sex,@RequestParam("typeId")String typeId){
        name = (name == null || name.equals("undefined")) ? "" : name;
        sex = (sex == null || sex.equals("undefined")) ? "" : sex;
        if(Objects.equals(typeId, "") || typeId == null || typeId.equals("undefined")){
            return studentRepository.findAllByNameLikeAndSexLike("%"+name+"%","%"+sex+"%");
        }
        return studentRepository.findAllByNameLikeAndSexLikeAndTypeIdLike("%"+name+"%","%"+sex+"%", Long.valueOf(typeId));
    }

    /** 学生新增修改 **/
    @PostMapping("/studSave")
    public int studSave(@RequestBody StStudent student) {
        if(Objects.equals(student.getName(), "") ||student.getName()==null || student.getName().equals("undefined")|| student.getTypeId()==null
        || student.getBirthday()==null
        ){
            return 501;
        }
        try {
            studentRepository.save(student);
        } catch (Exception e) {
            return 500;
        }
        return 200;
    }

    /** 学生删除 **/
    @PostMapping("/studRemove")
    @ResponseBody
    public int studRemove(@RequestParam("ids") Long[] ids){
        try {
            studentRepository.deleteByIds(Arrays.asList(ids));
        } catch (DataIntegrityViolationException e){
            SQLException sqlException = (SQLException) e.getCause().getCause(); // 获取SQL异常
            return sqlException.getErrorCode(); //数据库错误码
        } catch (Exception e){
            return 500;
        }
        return 200;
    }
}
